﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OntologyUtility;

namespace OntologyFormDesigner.GuiComponent
{
    class OntologyChooseNoTree : WizardPanel
    {
        #region private vars
        private System.ComponentModel.IContainer components = null;
        private TreeView tree;
        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel;
        private TreeNode selectedNode { get; set; }
        #endregion

        public OntologyChooseNoTree(WizardForm wizard, Control control)
            : base(control)
        {
            this.Parent = wizard;
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {

            // 
            // OntologiesChoose
            // 
            this.Name = "OntologiesChoose";
            this.Size = new System.Drawing.Size(800, 600);
            this.ResumeLayout(false);

        }

        public override void createControl()
        {
            buildPanel();
        }

        public void buildPanel()
        {
            this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
            this.tableLayoutPanel.ColumnCount = 3;

            this.SuspendLayout();
            // 
            // tableLayoutPanel3
            // 
            
            int index = 0;
            foreach (Node node in OntologyModel.Instance.OntologyNode.Childs)
            {
                this.tableLayoutPanel.ColumnCount = node.Childs.Count;
                this.tableLayoutPanel.BackColor = UIUtil.COLOR_BLUE;
                for (int i = 0; i < node.Childs.Count; i++)
                    this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,
                        (100F / node.Childs.Count)));

                this.tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
                this.tableLayoutPanel.Location = new System.Drawing.Point(0, 0);
                this.tableLayoutPanel.Name = "tableLayoutPanel3";
                this.tableLayoutPanel.RowCount = 1;
                this.tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tableLayoutPanel.Size = new System.Drawing.Size(545, 415);
                this.tableLayoutPanel.TabIndex = 0;
                this.Controls.Add(this.tableLayoutPanel);

               
                foreach (Node _node in node.Childs)
                {
                    TableLayoutPanel panel = new TableLayoutPanel()
                    {
                        Dock = DockStyle.Fill,
                        RowCount = 2,
                        ColumnCount = 1
                    };
                    
                    panel.Controls.Add(
                        new Label()
                        {
                            Text = _node.Name,
                            Dock = DockStyle.Fill,
                            BackColor = UIUtil.COLOR_YELLOW,
                            ForeColor = UIUtil.COLOR_BLUE,
                            TextAlign = System.Drawing.ContentAlignment.MiddleCenter
                        }
                    );

                    tree = UIUtil.createFlatTreeView(_node, Control.GetType());
                    tree.Dock = DockStyle.Fill;
                    tree.AfterSelect += new TreeViewEventHandler(this.tree_AfterSelect);
                    panel.Controls.Add(tree);

                    tableLayoutPanel.Controls.Add(panel, (index++), 0);
                }

                
            }

            
        }

        public void tree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                selectedNode = ((TreeView)sender).SelectedNode;
                this.PageComplete = true;
                this.canFlipToNextPage();
                this.NextPage.ObjectWizard = ((TreeView)sender).SelectedNode.Tag;
            }
            catch (Exception)
            {
                this.PageComplete = false;
                this.canFlipToNextPage();
                Parent.DisableNext();
            }
        }

        public override void Next()
        {
            try
            {
                ((Element)Parent.Tag).Container.Node = (Node)selectedNode.Tag;
            }
            catch (Exception ex)
            {
                MessageBox.Show("OntologiesChoose.cs " + ex.ToString());
            }
        }
    }
}
